define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'rush/order/index' + location.search,
                    add_url: 'rush/order/add',
                    edit_url: 'rush/order/edit',
                    del_url: 'rush/order/del',
                    multi_url: 'rush/order/multi',
                    import_url: 'rush/order/import',
                    table: 'rush_order',
                }
            });

            var table = $("#table");

            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //总付款流水
                $("#pay_total").text(data.pay_total);
                //今日付款流水
                $("#pay_today").text(data.pay_today);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'order_status', title: __('Order_status'), searchList: {"0":__('Order_status 0'),"1":__('Order_status 1'),"2":__('Order_status 2'),"3":__('Order_status 3'),"4":__('Order_status 4'),"5":__('Order_status 5')}, formatter: Table.api.formatter.status},
                        {field: 'payment_voucher', title: __('Payment_voucher'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'complain_status', title: __('Complain_status'), searchList: {"0":__('Complain_status 0'),"1":__('Complain_status 1')}, formatter: Table.api.formatter.status},
                        {field: 'express_company', title: __('Express_company'), operate: 'LIKE'},
                        {field: 'express_no', title: __('Express_no'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'rushtime', title: __('Rushtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'confirmtime', title: __('Confirmtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'goods_name', title: __('Goods_name'), operate: 'LIKE'},
                        {field: 'goods_price', title: __('Goods_price'), operate:'BETWEEN'},
                        {field: 'goods_image', title: __('Goods_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'time_name', title: __('Time_name'), operate: 'LIKE'},
                        {field: 'seller.nickname', title: __('Seller.nickname'), operate: 'LIKE'},
                        {field: 'seller.mobile', title: __('Seller.mobile'), operate: 'LIKE'},
                        {field: 'buyer.nickname', title: __('Buyer.nickname'), operate: 'LIKE'},
                        {field: 'buyer.mobile', title: __('Buyer.mobile'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'buyerCancel',
                                    text: __('取消订单'),
                                    title: __('取消订单'),
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    icon: 'fa',
                                    url: 'rush/order/buyerCancel?ids={id}',
                                    visible: function(row){
                                        return row.order_status == '0' ? true : false;
                                    },
                                    confirm: '确认发送',
                                    success: function (data, ret) {
                                        //Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'buyerPay',
                                    text: __('确认支付'),
                                    title: __('确认支付'),
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    icon: 'fa',
                                    url: 'rush/order/buyerPay?ids={id}',
                                    visible: function(row){
                                        return row.order_status == '0' ? true : false;
                                    },
                                    confirm: '确认发送',
                                    success: function (data, ret) {
                                        //Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'buyerNoPay',
                                    text: __('买家未支付'),
                                    title: __('买家未支付'),
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    icon: 'fa',
                                    url: 'rush/order/buyerNoPay?ids={id}',
                                    visible: function(row){
                                        return row.order_status == '1' ? true : false;
                                    },
                                    confirm: '确认发送',
                                    success: function (data, ret) {
                                        //Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'sellerGetMoney',
                                    text: __('确认收款'),
                                    title: __('确认收款'),
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    icon: 'fa',
                                    url: 'rush/order/sellerGetMoney?ids={id}',
                                    visible: function(row){
                                        return row.order_status == '1' ? true : false;
                                    },
                                    confirm: '确认发送',
                                    success: function (data, ret) {
                                        //Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'sellerNoGetMoney',
                                    text: __('确认未收款'),
                                    title: __('确认未收款'),
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    icon: 'fa',
                                    url: 'rush/order/sellerNoGetMoney?ids={id}',
                                    visible: function(row){
                                        return row.order_status == '2' ? true : false;
                                    },
                                    confirm: '确认发送',
                                    success: function (data, ret) {
                                        //Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'buyerUp',
                                    text: __('上架'),
                                    title: __('上架'),
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    icon: 'fa',
                                    url: 'rush/order/buyerUp?ids={id}',
                                    visible: function(row){
                                        return row.order_status == '2' ? true : false;
                                    },
                                    confirm: '确认发送',
                                    success: function (data, ret) {
                                        //Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'buyerTakeover',
                                    text: __('提货'),
                                    title: __('提货'),
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    icon: 'fa',
                                    url: 'rush/order/buyerTakeover?ids={id}',
                                    visible: function(row){
                                        return row.order_status == '2' ? true : false;
                                    },
                                    confirm: '确认发送',
                                    success: function (data, ret) {
                                        //Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'buyerTakeoverCancel',
                                    text: __('取消提货'),
                                    title: __('取消提货'),
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    icon: 'fa',
                                    url: 'rush/order/buyerTakeoverCancel?ids={id}',
                                    visible: function(row){
                                        return row.order_status == '4' ? true : false;
                                    },
                                    confirm: '确认发送',
                                    success: function (data, ret) {
                                        //Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'express',
                                    text: __('填写物流信息'),
                                    title: __('填写物流信息'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa',
                                    url: 'rush/order/express',
                                    visible: function (row){
                                        return (row.order_status == '4' && row.express_no == '') ? true : false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        editstatus: function () {
            Controller.api.bindevent();
        },
        express: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
